package main.java.com.epam;

import java.util.ArrayList;
import java.util.List;

public class MersennePrime {

	public static List<Long> fintFirstNMersennerPrimeNumbers(int n){
		
		if (n > 18 || n < 1) throw new RuntimeException();
		
		List<Long> mersennerPrimeNumbers = new ArrayList<Long>();
		
		//getting first n primary numbers
		List<Integer> primaryNumbers = getPrimaryNumbers(n);
		
		//count Mersenne prime numbers MP = 2^p - 1 where p - primary number
		for(Integer primaryNumber:primaryNumbers){
			mersennerPrimeNumbers.add((long)Math.pow(2, primaryNumber) - 1);
		}
		return mersennerPrimeNumbers;
	}
	
	//getting first n primary numbers
	private static List<Integer> getPrimaryNumbers(int numbersAmount){
		
		List<Integer> primaryNumbers = new ArrayList<Integer>();
		primaryNumbers.add(2);
		int i = 3;
		while(primaryNumbers.size() < numbersAmount) {
			for(int j = 0; j < primaryNumbers.size(); j++){
				if (i % primaryNumbers.get(j) == 0) break;
				else if(j == primaryNumbers.size() - 1) primaryNumbers.add(i);
			}
			i += 2;
		}
		return primaryNumbers;
	}

}
